

package com.sakila.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sakila.dbo.StaffDBO;

import com.sakila.util.DBConnection;

public class StaffDAO implements IStaffDAO {








	  private Logger logger = Logger.getLogger(this.getClass());
	//SimpleDateFormat formatter = new SimpleDateFormat(
		//	"yyyy.MM.dd G 'at' HH:mm:ss z");

	/* (non-Javadoc)
	 * @see com.sakila.dao.IStaffDAO#findById(int)
	 */
	@Override
	public StaffDBO findById(int staffId) {
		StaffDBO dbo = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select staff_id as staffId, firstName as firstName, lastName as lastName, address_id as addressId,  picture as picture, email as email, store_id as storeId, active as active, username as userName, password as password, last_update as lastUpdate");
			sb.append("from staff ");
			sb.append("where staff_id = " + staffId);
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here in StaffDAO: "+ sb.toString());
			rs = statement.executeQuery();
			if (rs.next()) {
				dbo = new StaffDBO();
				   
				dbo.setStaff_id(rs.getInt("staffId"));
				dbo.setFirstName(rs.getString("firstName"));
				dbo.setLastName(rs.getString("lastName"));
				dbo.setAddress_id(rs.getInt("addressId"));
				dbo.setPicture(rs.getString("picture"));
				dbo.setEmail(rs.getString("email"));
				    
				dbo.setStore_id(rs.getInt("storeId"));
				dbo.setActive(rs.getInt("active"));
				dbo.setUsername(rs.getString("userName"));
				dbo.setPassword(rs.getString("password"));
				
				dbo.setLast_update(rs.getDate("lastUpdate"));
				
			}

		} catch (SQLException e) {
			logger.error("Exception in StaffDAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
		return dbo;
	}

	/* (non-Javadoc)
	 * @see com.sakila.dao.IStaffDAO#findAll()
	 */
	@Override
	public List<StaffDBO> findAll() {
		List<StaffDBO> returnList = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		StaffDBO dbo = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select staff_id as staffId, firstName as firstName, lastName as lastName, address_id as addressId,  picture as picture, email as email, store_id as storeId, active as active, username as userName, password as password, last_update as lastUpdate");
			sb.append("from staff ");
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here StaffDAO infindAll: "+ sb.toString());
			rs = statement.executeQuery();
			returnList = new ArrayList<StaffDBO>();
			while (rs.next()) {
				dbo = new StaffDBO();
				dbo.setStaff_id(rs.getInt("staffId"));
				dbo.setFirstName(rs.getString("firstName"));
				dbo.setLastName(rs.getString("lastName"));
				dbo.setAddress_id(rs.getInt("addressId"));
				dbo.setPicture(rs.getString("picture"));
				dbo.setEmail(rs.getString("email"));
				    
				dbo.setStore_id(rs.getInt("storeId"));
				dbo.setActive(rs.getInt("active"));
				dbo.setUsername(rs.getString("userName"));
				dbo.setPassword(rs.getString("password"));
				
				dbo.setLast_update(rs.getDate("lastUpdate"));
				returnList.add(dbo);
				
			}

		} catch (SQLException e) {
			logger.error("Exception in StaffDAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
logger.debug("Display Me everyone from List:" + returnList);
		return returnList;
	}
	
}

